import * as fs from "fs";
import * as objectPath from "object-path";
import * as path from "path";

export class Manifest {
  private static inst: Manifest | null = null;
  private file = "";
  private _cfg: any = {};

  get cfg() {
    return this._cfg;
  }

  static getInstance() {
    if (this.inst === null) {
      this.inst = new Manifest();
    }
    return this.inst;
  }

  constructor() {
    this.file = path.resolve(process.cwd(), "./manifest.json");
    this._cfg = JSON.parse(fs.readFileSync(this.file, "utf-8"));
  }

  get<T>(keyPath: string) {
    return objectPath.get<T, T>(this._cfg, keyPath);
  }

  set(keyPath: string, v: any) {
    objectPath.set(this._cfg, keyPath, v);
    fs.writeFileSync(this.file, JSON.stringify(this._cfg));
  }
}
